package com.oauth.linkedin.client.ui.tabs;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.linkedin.client.beans.LinkedinUser;
import com.oauth.linkedin.client.rpc.LinkedinRemoteServiceAsync;
import com.oauth.linkedin.client.ui.ConnectionPanel;

public class InboxTab extends Composite {

	private static InboxTabUiBinder uiBinder = GWT
			.create(InboxTabUiBinder.class);

	interface InboxTabUiBinder extends UiBinder<Widget, InboxTab> {
	}

	@UiField
	VerticalPanel inboxPanel;

	@UiField
	Image loadingImage;

	@UiField
	HorizontalPanel loadingPanel;

	private LinkedinRemoteServiceAsync service;

	public InboxTab(LinkedinRemoteServiceAsync pService) {
		service = pService;

		initWidget(uiBinder.createAndBindUi(this));
		loadingPanel.setCellHorizontalAlignment(loadingImage,
				VerticalPanel.ALIGN_CENTER);
		pService.getConnections(new OAuthAsnyCallBack<List<LinkedinUser>>() {

			@Override
			public void onSuccess(List<LinkedinUser> pResult) {
				if (pResult.isEmpty()) {
					inboxPanel.add(new Label("You don't have any connections"));
				} else {
					for (LinkedinUser user : pResult) {
						inboxPanel.add(new ConnectionPanel(user));
					}
				}
				loadingImage.setVisible(false);
			}

		});
	}

	public void init() {
		loadingImage.setVisible(true);

		inboxPanel.getElement().setInnerHTML("");

		service.getConnections(new OAuthAsnyCallBack<List<LinkedinUser>>() {

			@Override
			public void onSuccess(List<LinkedinUser> pResult) {
				if (pResult.isEmpty()) {
					inboxPanel.add(new Label(
							"You don't have any message in your inbox"));
				} else {
					for (LinkedinUser user : pResult) {
						inboxPanel.add(new ConnectionPanel(user));
					}
				}
				loadingImage.setVisible(false);
			}

		});
	}
}
